/**
 * @description 生成随机数
 * @param {Number} min 最小值
 * @param {Number} max 最大值
 * @return number
 */
export function randomNum(min, max) {
	let num = Math.floor(Math.random() * (min - max) + max);
	return num;
}

/**
 * @description 返回字符串的最后一个值
 * @param {String} str 字符串
 * @return String
 */
export function getStringOne(str) {
	return str.charAt(str.length - 1);;
}

/**
 * @description 返回一个树结构
 * @param {Array} arr 数组
 * @return tree
 */
export function arrayToTree(arr, args) {
	const map = {};
	const roots = [];
	for (const item of arr) {
		map[item.id] = { ...item, children: [] };
	}
	for (const item of arr) {
		const parent = map[item.pid];
		if (parent) {
			parent.children.push({ ...map[item.id], ...args });
		} else {
			roots.push({ ...map[item.id], ...args });
		}
	}
	return roots;
	// tenantId
}
/**
 * 格式化时间戳 Y-m-d H:i:s
 * @param {String} format Y-m-d H:i:s
 * @param {Number} timestamp 时间戳   
 * @return {String}
 */
export const date = (format, timeStamp) => {
	if ('' + timeStamp.length <= 10) {
		timeStamp = + timeStamp * 1000;
	} else {
		timeStamp = + timeStamp;
	}
	let _date = new Date(timeStamp),
		Y = _date.getFullYear(),
		m = _date.getMonth() + 1,
		d = _date.getDate(),
		H = _date.getHours(),
		i = _date.getMinutes(),
		s = _date.getSeconds();

	m = m < 10 ? '0' + m : m;
	d = d < 10 ? '0' + d : d;
	H = H < 10 ? '0' + H : H;
	i = i < 10 ? '0' + i : i;
	s = s < 10 ? '0' + s : s;

	return format.replace(/[YmdHis]/g, key => {
		return { Y, m, d, H, i, s }[key];
	});
}
/**
 * 数字金额转大写 
 * @param {Number} amount 金额   
 * @return {String}
 */
export function convertToChineseNumber(n) {
	var fraction = ['角', '分'];
	var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
	var unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']];
	var head = n < 0 ? '欠' : '';
	n = Math.abs(n);

	var s = '';

	for (var i = 0; i < fraction.length; i++) {
		s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
	}
	s = s || '整';
	n = Math.floor(n);

	for (var i = 0; i < unit[0].length && n > 0; i++) {
		var p = '';
		for (var j = 0; j < unit[1].length && n > 0; j++) {
			p = digit[n % 10] + unit[1][j] + p;
			n = Math.floor(n / 10);
		}
		s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
	}
	return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}

/* 格式化日期 */

export const formatDate = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();

	return `${year}-${month > 9 ? month : '0' + month}-${day > 9 ? day : '0' + day}`
}

/**
 * 对账列表跳转其他页面封装
 * @param {String} type 类型   
 * @param {String || Number} id ID   
 * @param {String || null} item 数据 
 * @param {String || null} this 当前对象 
 * @return {null}
 */
export const reconciliationGorouter = (type, id, _this, item = null) => {

	switch (type) {
		case 'finAdjust':
			_this.$Router.push({
				path: "/pages/application/pages/finance/accounts-receivable-payable/add-business-reconciliation",
				query: {
					id: id
				}
			});
			break;
		case 'sale':
			uni.redirectTo({
				url: `/pages/application/pages/cloth/procure/settlement/sales?id=${id}&type=details`
			})
			break;
		case 'finReceiveBill':
			_this.$Router.push({
				path: "/pages/application/pages/finance/accounts-receivable-payable/add-receipt",
				query: {
					id: id
				}
			});
			break;
		case 'purchase':
			uni.redirectTo({
				url: `/pages/application/pages/cloth/procure/settlement/index?id=${id}&type=details`
			})
			break;
		case 'finPayBill':
			uni.redirectTo({
				url: `/pages/application/pages/finance/accounts-receivable-payable/add-payment-order?id=${id}`
			})
			break;

	}

}